<?php
/**
 * Create by 11li
 * Date 2020/1/10
 * Time 15:28
 *
 */

namespace app\api\controller\v2;


use app\common\controller\Api;
use app\common\model\AgentShop;
use think\Db;

class Index extends Api
{
	/**
	 * 提货点
	 */
	public function agent() {
		$lat = $this->request->post('lat');
		$lng = $this->request->post('lng');
		$page = $this->request->post('page', 1, 'intval');
		
		$userModel = new \app\admin\model\User();
		
		$where =[
			'role' => ['in', [$userModel::LEVEL_WAREHOUSE, $userModel::LEVEL_SHOP]],
			'area' => $this->area,
			'status' => AgentShop::STATUS_Y
		];
		
		$total  = Db::name('agentShop')
			->where($where)
			->count();
		
		$agents = Db::name('agentShop')
			->where($where)
			->field('id, name, image, content, mobile, lat, lng, address, role, ROUND(6378.138 * 2 * ASIN(SQRT( POW( SIN( ('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW( SIN(('.$lng.' * PI() / 180 - lng * PI() / 180 ) / 2 ),  2))) * 1000) AS distance')
			->order('distance asc')
			->page($page)
			->select();
		
		if($agents) {
			foreach ($agents as &$agent) {
				$agent['distance'] = get_distance($agent['distance']);
			}
		}
		$this->success('查询成功', [
			'total' => $total,
			'list' => $agents
		]);
	}
}